<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            padding: 0%;
        }
    </style>
</head>

<body>
    <div class="box">
        <button class="btn">开关灯</button> 
    </div>
    
    <script>
        var btn =document.querySelector('.btn');
        var flag = 0;
        var body = document.querySelector('body');
        btn.onclick = function(){
            if(flag ==0){
            body.style.backgroundColor = 'black';
            flag = 1;
            }else{
                body.style.backgroundColor='';
                flag = 0;
            }
        }
    </script>
</body>
</html>